package feeinterface.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ulwx.database.DataBaseSet;
import com.ulwx.tool.DbUtils;
import com.ulwx.tool.ObjectUtils;
import com.ulwx.type.TInteger;

import feeinterface.model.ChannelModel;
import feeinterface.modeldb.ChannelServiceCoUserLimt;
import feeinterface.utils.DaoUtil;

public class ChannelServiceCoUserLimtDao {
	private static final Logger log = Logger
			.getLogger(ChannelServiceCoUserLimtDao.class);

	public static ChannelServiceCoUserLimt getBy(Integer ServiceCo, String IMSI)
			throws Exception {
		String sql = "select ServiceCoID,IMSI, IFNULL(sum(MonthFee),0) as MonthFee"
				+ ",IFNULL(sum(DayFee),0) as DayFee,"
				+ "CAST(IFNULL(max(LastFeeTime),0)  as date) as LastFeeTime, 0 as channelID, " +
				"0 as channelServiceCoUserLimitSno"
				+ " from ChannelServiceCoUserLimt where "
				+ "ServiceCoID=? and  IMSI=? ";

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, ServiceCo);
		map.put(2, IMSI);

		List<ChannelServiceCoUserLimt> rs = DbUtils.doQueryClass(null, ChannelServiceCoUserLimt.class,
				 sql, map);
		if (rs.size() > 0){
		
			
			return rs.get(0);
		}
		
		return null;
	}

	public static void main(String[] args) throws Exception{
		getBy(10033,"460021088885182");
	}
	public static void insertUpdateLastFeeTime(Date curtime, Integer ChannelID,
			Integer ServiceCoID, String IMSI) {

		String sql = "insert into ChannelServiceCoUserLimt(ServiceCoID,ChannelID,"
				+ "IMSI,LastFeeTime)values(?,?,?,?) ON DUPLICATE KEY UPDATE LastFeeTime=?";
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, ServiceCoID);
		map.put(2, ChannelID);
		map.put(3, IMSI);
		map.put(4, curtime);
		map.put(5, curtime);
		try {
			DbUtils.executeBindUpdate(null, sql, map);
		} catch (Exception e) {
			log.error("", e);
		}
	}





}
